package com.quicksignin.controller;

import com.alibaba.fastjson.JSONObject;
import com.quicksignin.common.Const;
import com.quicksignin.pojo.SignIn;
import com.quicksignin.pojo.User;
import com.quicksignin.service.IResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by GHD on 2018/10/20.
 */
@Controller
@RequestMapping("")
public class ResultController {


    @Autowired
    IResultService iResultService;

    @RequestMapping("result")
    public String showResult(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        System.out.println("展示签到结果页面");
        return "result";
    }

    @GetMapping("teacherList")
    @ResponseBody
    public List<String> showTeacherList() {
        List<String> response = iResultService.getTeacherList();
        return response;
    }

    @GetMapping("allresult")
    @ResponseBody
    public JSONObject result(HttpSession session) {
        JSONObject response = new JSONObject();
        List<SignIn> result = iResultService.result();
        response.put("allSignIner", result);
        return response;
    }

    @PostMapping("userResultsub")
    @ResponseBody
    public JSONObject getUserResult(String stuName){
        JSONObject response = new JSONObject();
        List<SignIn> userResult = iResultService.getUserResult(stuName);
        response.put("userResult",userResult);
        return response;
    }


    @PostMapping("resultsub")
    @ResponseBody
    public JSONObject getResult(HttpSession session, String teacherName, String classDate, String classTime, int times) throws ParseException {
        System.out.println(teacherName + classDate + classTime + times);
        JSONObject response = new JSONObject();

        List<SignIn> result = iResultService.getResult(teacherName, classDate);
//        System.out.println(result);
        List<SignIn> sec_result = new ArrayList<SignIn>();
        List<SignIn> last_result = new ArrayList<SignIn>();
////        last_result.clear();
//        if (result.equals(null)) {
//            last_result.add(result.get(0));
//        }
//
//        //早退
//        for (int i = 0; i < result.size(); i++) {
//            int x = 0;
//            for (int j = 0; j < result.size(); j++) {
//                if (result.get(i).getStuId().equals(result.get(j).getStuId()) && result.get(i).getIpAddr().equals(result.get(j).getIpAddr())) {
//                    x++;
//                }
//
//            }
//            if (x < times) {
//                result.get(i).setStatus("早退");
//            }
//            Integer y = 0;
//            for (int m = 0; m < last_result.size(); m++) {
////                System.out.println(result.get(i).getStuId());
////                System.out.println(last_result.get(m).getStuId());
//                //true
//                if (result.get(i).getStuId().equals(last_result.get(m).getStuId()) && result.get(i).getIpAddr().equals(last_result.get(m).getIpAddr())) {
//                    y++;
//                }
//
//            }
//            if (y.equals(0)) {
////                System.out.println("添加一个");
//                last_result.add(result.get(i));
//            }
//
//        }
//        System.out.println(result);
//
//        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
//        SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//        String s_time = df.format(df.parse(Const.SPEC_TIME));
//        String sp_time = classDate + " " + s_time;
//        Date spec_time = d1.parse(sp_time);
//
//        String class_1_time = df.format(df.parse(Const.CLASS_1_BEGINTIME));
//        String class_1_begTime = classDate + " " + class_1_time;
//        Date class_1_beginTime = d1.parse(class_1_begTime);
//
//        String class_1_etime = df.format(df.parse(Const.CLASS_1_ENDTIME));
//        String class_1_enTime = classDate + " " + class_1_etime;
//        Date class_1_endTime = d1.parse(class_1_enTime);
//
//        String class_2_time = df.format(df.parse(Const.CLASS_2_BEGINTIME));
//        String class_2_begTime = classDate + " " + class_2_time;
//        Date class_2_beginTime = d1.parse(class_2_begTime);
//
//        String class_2_etime = df.format(df.parse(Const.CLASS_2_ENDTIME));
//        String class_2_enTime = classDate + " " + class_2_etime;
//        Date class_2_endTime = d1.parse(class_1_enTime);
//
//        String class_3_time = df.format(df.parse(Const.CLASS_3_BEGINTIME));
//        String class_3_begTime = classDate + " " + class_3_time;
//        Date class_3_beginTime = d1.parse(class_3_begTime);
//
//        String class_3_etime = df.format(df.parse(Const.CLASS_3_ENDTIME));
//        String class_3_enTime = classDate + " " + class_3_etime;
//        Date class_3_endTime = d1.parse(class_3_enTime);
//
//        String class_4_time = df.format(df.parse(Const.CLASS_4_BEGINTIME));
//        String class_4_begTime = classDate + " " + class_4_time;
//        Date class_4_beginTime = d1.parse(class_4_begTime);
//
//        String class_4_etime = df.format(df.parse(Const.CLASS_4_ENDTIME));
//        String class_4_enTime = classDate + " " + class_4_etime;
//        Date class_4_endTime = d1.parse(class_4_enTime);
//
//
//        for (int n = last_result.size() - 1; n >= 0; n--) {
//            if (last_result.get(n).getStatus() != null) {
//                continue;
//            }
//
//            Date signTime = d1.parse(d1.format(last_result.get(n).getSignTime()));
////            System.out.println(df.format(signTime));
//
//            if (classTime.equals("1")) {
//                System.out.println("第一节课");
//                if (signTime.after(spec_time) && signTime.before(class_1_endTime)) {
//                    if (signTime.after(class_1_beginTime)) {
//                        last_result.get(n).setStatus("迟到");
//                    } else {
//                        last_result.get(n).setStatus("正常");
//                    }
//                } else {
//                    System.out.println("移除");
//                    last_result.remove(last_result.get(n));
//                }
//
//
//            } else if (classTime.equals("2")) {
//                System.out.println("第二节课");
//                if (signTime.after(class_1_endTime) && signTime.before(class_2_endTime)) {
//                    if (signTime.after(class_2_beginTime)) {
//                        last_result.get(n).setStatus("迟到");
//                    } else {
//                        last_result.get(n).setStatus("正常");
//                    }
//                } else {
//                    System.out.println("移除");
//                    last_result.remove(last_result.get(n));
//                }
//
//
//            } else if (classTime.equals("3")) {
//                System.out.println("第三节课");
//                if (signTime.after(class_2_endTime) && signTime.before(class_3_endTime)) {
//                    if (signTime.after(class_3_beginTime)) {
//                        last_result.get(n).setStatus("迟到");
//                    } else {
//                        last_result.get(n).setStatus("正常");
//                    }
//                } else {
//                    System.out.println("移除");
//                    last_result.remove(last_result.get(n));
//                }
//
//            } else if (classTime.equals("4")) {
//                System.out.println("第四节课");
//                if (signTime.after(class_3_endTime) && signTime.before(class_4_endTime)) {
//                    if (signTime.after(class_4_beginTime)) {
//                        last_result.get(n).setStatus("迟到");
//                    } else {
//                        last_result.get(n).setStatus("正常");
//                    }
//                } else {
//                    System.out.println("移除");
//                    last_result.remove(last_result.get(n));
//                }
//
//
//            }
//        }
//        System.out.println(last_result);


        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String s_time = df.format(df.parse(Const.SPEC_TIME));
        String sp_time = classDate + " " + s_time;
        Date spec_time = d1.parse(sp_time);

        String class_1_time = df.format(df.parse(Const.CLASS_1_BEGINTIME));
        String class_1_begTime = classDate + " " + class_1_time;
        Date class_1_beginTime = d1.parse(class_1_begTime);

        String class_1_etime = df.format(df.parse(Const.CLASS_1_ENDTIME));
        String class_1_enTime = classDate + " " + class_1_etime;
        Date class_1_endTime = d1.parse(class_1_enTime);

        String class_2_time = df.format(df.parse(Const.CLASS_2_BEGINTIME));
        String class_2_begTime = classDate + " " + class_2_time;
        Date class_2_beginTime = d1.parse(class_2_begTime);

        String class_2_etime = df.format(df.parse(Const.CLASS_2_ENDTIME));
        String class_2_enTime = classDate + " " + class_2_etime;
        Date class_2_endTime = d1.parse(class_2_enTime);

        String class_3_time = df.format(df.parse(Const.CLASS_3_BEGINTIME));
        String class_3_begTime = classDate + " " + class_3_time;
        Date class_3_beginTime = d1.parse(class_3_begTime);

        String class_3_etime = df.format(df.parse(Const.CLASS_3_ENDTIME));
        String class_3_enTime = classDate + " " + class_3_etime;
        Date class_3_endTime = d1.parse(class_3_enTime);

        String class_4_time = df.format(df.parse(Const.CLASS_4_BEGINTIME));
        String class_4_begTime = classDate + " " + class_4_time;
        Date class_4_beginTime = d1.parse(class_4_begTime);

        String class_4_etime = df.format(df.parse(Const.CLASS_4_ENDTIME));
        String class_4_enTime = classDate + " " + class_4_etime;
        Date class_4_endTime = d1.parse(class_4_enTime);

        for (int n = result.size() - 1; n >= 0; n--) {

            Date signTime = d1.parse(d1.format(result.get(n).getSignTime()));

            if (classTime.equals("1")) {
                System.out.println("第一节课");
                if (signTime.after(spec_time) && signTime.before(class_1_endTime)) {
                    sec_result.add(result.get(n));
                }

            } else if (classTime.equals("2")) {
                System.out.println("第二节课");
                if (signTime.after(class_1_endTime) && signTime.before(class_2_endTime)) {
                    sec_result.add(result.get(n));
                }


            } else if (classTime.equals("3")) {
                System.out.println("第三节课");
                if (signTime.after(class_2_endTime) && signTime.before(class_3_endTime)) {
                    System.out.println(signTime);
                    System.out.println(class_2_endTime);
                    System.out.println(signTime.after(class_2_endTime));
                    sec_result.add(result.get(n));
                }

            } else if (classTime.equals("4")) {
                System.out.println("第四节课");
                if (signTime.after(class_3_endTime) && signTime.before(class_4_endTime)) {
                    sec_result.add(result.get(n));
                }
            }
        }
//        System.out.println(sec_result);

        if (!sec_result.isEmpty()) {
            last_result.add(sec_result.get(sec_result.size() - 1));
        }
        for (int i = sec_result.size() - 1; i > -1; i--) {
            int x = 0;
            if (sec_result.isEmpty()) {
                break;
            }
            for (int j = sec_result.size() - 1; j > -1; j--) {
                if (sec_result.get(i).getStuId().equals(sec_result.get(j).getStuId()) && sec_result.get(i).getIpAddr().equals(sec_result.get(j).getIpAddr())) {
                    x++;
                }

            }
            if (x < times) {
                sec_result.get(i).setStatus("早退");
            }

            Integer y = 0;
            for (int m = 0; m < last_result.size(); m++) {
                //true
//                System.out.println("asdasd" + last_result.get(0).getStuId());
//                System.out.println(sec_result.get(i).getStuId());
//                System.out.println(last_result.get(m).getStuId());
                if (sec_result.get(i).getStuId().equals(last_result.get(m).getStuId()) && sec_result.get(i).getIpAddr().equals(last_result.get(m).getIpAddr())) {
                    y++;
                }

            }
            if (y.equals(0)) {
//                System.out.println(last_result);
//                System.out.println("添加一个");
                last_result.add(sec_result.get(i));
            }

        }
//        System.out.println("最新数据"+last_result);


        for (int k = 0; k < last_result.size(); k++) {

            if (last_result.get(k).getStatus() != null) {
                continue;
            }

            Date signTime = d1.parse(d1.format(last_result.get(k).getSignTime()));

            if (classTime.equals("1")) {
                System.out.println("第一节课");
                if (signTime.before(class_1_beginTime)) {
                    last_result.get(k).setStatus("正常");
                } else {
                    last_result.get(k).setStatus("迟到");
                }

            } else if (classTime.equals("2")) {
                System.out.println("第二节课");
                if (signTime.before(class_2_beginTime)) {
                    last_result.get(k).setStatus("正常");
                } else {
                    last_result.get(k).setStatus("迟到");
                }


            } else if (classTime.equals("3")) {
                System.out.println("第三节课");
                if (signTime.before(class_3_beginTime)) {
                    last_result.get(k).setStatus("正常");
                } else {
                    last_result.get(k).setStatus("迟到");
                }

            } else if (classTime.equals("4")) {
                System.out.println("第四节课");
                System.out.println(d1.format(result.get(k).getSignTime()));
                System.out.println(class_4_enTime);
                System.out.println(signTime.getTime());
                System.out.println(class_4_beginTime.getTime());
                if (signTime.before(class_4_beginTime)) {
                    last_result.get(k).setStatus("正常");
                } else {
                    last_result.get(k).setStatus("迟到");
                }
            }
        }
        response.put("signIner", last_result);


        return response;

    }


}
